import React, {Component} from 'react';
import UserTable from './UserTable';
import http from '../../http';
import Pagination from "../Public/Pagination";

class Index extends Component {
	constructor(props) {
		super(props);
		this.state = {
			pageNum: 1,
			pageSize: 10,
			total: 0,
			list: []
		}
	}

	componentDidMount() {
		this.getUserList()
	}

	changePageNum = async pageNum => {
		await this.setState({
			pageNum
		});
		this.getUserList();
	};

	getUserList = () => {
		http.getUserList(this.state.pageNum).then(res => {
			if (res.status === 0) {
				this.setState({
					total: res.data.total,
					pageSize: res.data.pageSize,
					list: res.data.list
				})
			} else if (res.status === 10) {
				alert(res.msg);
				this.props.history.push('/login');
			}
		})
	};

	render() {
		return (
			<div>
				<UserTable data={this.state.list}/>
				<Pagination
					pageSize={this.state.pageSize}
					pageNum={this.state.pageNum}
					total={this.state.total}
					onChange={this.changePageNum}
				/>
			</div>
		);
	}
}

export default Index;